home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_03 / 1103089a < prev    next >
Text File  |  1993-01-04  |  3KB  |  79 lines

  1. /*
  2. * This implementation of the above algorithm has some distinct   
  3. * limitations, although I believe they are not a serious problem.   
  4. * This is intended for processing short strings of digits (10 or so),   
  5. * not arbitrarily long digit strings. Each of the substrings that are   
  6. * broken out by the code below are assumed to be a value that can   
  7. * be assigned to a long when converted to an integral type. This   
  8. * limitation requires that the input string not be more than about 20   
  9. * characters in length, which is more than adequate for the intended   
  10. * application. I'm assuming that a long is at least 32 bits.      
  11. * Further, it assumes a character set where the digits' collating   
  12. * sequence mimics that of ASCII, so that things like:   
  13. *      x = val - '0';   
  14. * will evaluate to an integer value of 3 in the case where val == '3'.   
  15. */     
  16.  
  17. #include <stdio.h>     
  18. #define MOD10 10  
  19. #define MOD11 11        
  20. /*   
  21. * ----------------------------- check_digit() -------------------   *   
  22. * Calculates a check digit according to the algorithm given above. 
  23. * NOTE   
  24. * that there are no checks for array bounds overflow. Beware.   
  25. */     
  26.      check_digit (string, mod)      
  27.      char * string;      
  28.      int mod;      
  29.           {
  30.           char tmpbuf[20];      
  31.           int i, j, sum;      
  32.           unsigned long odd, even;         
  33.      /* extract the odd digits */      
  34.      j = strlen (string);      
  35.      for ( j--, i = 0 ; j >= 0 ; j -= 2, i++ )
  36.           {          
  37.           tmpbuf[i] = string[j];          
  38.           }
  39.      tmpbuf[i] = '\0';      
  40.      odd = atol (tmpbuf);         
  41.      /* extract the even digits */      
  42.      j  = strlen (string);      
  43.      for ( j -= 2, i = 0 ; j >= 0 ;  j -= 2, i++ )          
  44.           {
  45.           tmpbuf[i] = string[j];
  46.           }
  47.           tmpbuf[i] = '\0';      
  48.           even = atol (tmpbuf);         
  49.      /* now make the composite string & sum the digits */      
  50.      sprintf (tmpbuf, "%lu%lu", even, 2L * odd);      
  51.      i  = strlen (tmpbuf);      
  52.      for ( sum = j = 0 ; j < i ; j++)          
  53.           {
  54.           sum += tmpbuf[j] - '0';          
  55.           }
  56.      /* now do the mod10 or mod11 operation */  
  57.       i = (mod - (sum % mod));      
  58.      if (i == mod)          
  59.           i = 0;      
  60.      return (i);      
  61.      }
  62. #ifdef TEST  
  63.      main ()      
  64.      {
  65.       char buf[30];         
  66.      while (1)          
  67.           {
  68.           gets (buf);          
  69.           if (strlen (buf) == 0)  
  70.                break;          
  71.           printf ("string: %s, mod10 checkdigit: %d, "
  72.                "mod11 checkdigit %d:n",  
  73.                buf, check_digit (buf, MOD10), 
  74.                check_digit (buf, MOD11));          
  75.           }
  76.      }
  77. #endif /* TEST */     
  78.  
  79.